package com.auz.datagenerator.connector.heuristiclink;

import java.util.StringTokenizer;

/**
 * Class used to lex the equation provided to the heuristic link
 * @author Aurelien Bonnel
 */
public class Lexer {

	public static final int NUMBER_TOKEN = -1;
	public static final int EOLN_TOKEN = -2;
	
	private int token;
	private double number;
	private StringTokenizer tokens;

	/**
	 * Constructor.
	 * @param _equation - the equation to lex
	 */
	public Lexer(String _equation) {
		_equation = _equation.replace("E", "");
		tokens = new StringTokenizer(_equation, "\t\n\r+-*/()", true);
		read();
	}

	/**
	 * Reads the next token
	 */
	public void read() {
		while(true) {
			if(! tokens.hasMoreTokens()) {
				return;
			}

			String s = tokens.nextToken();
			char c = s.charAt(0);
			if((s.length() > 1 || Character.isDigit(c)) && ! Character.isLetter(c)) {
				number = Double.valueOf(s).doubleValue();
				token = NUMBER_TOKEN;
				return;
			} else if (! Character.isWhitespace(c) && ! Character.isLetter(c)) {
				token = c;
				return;
			}
		}
	}

	/**
	 * Returns the number
	 * @return the number
	 */
	public double getNum() {
		return number;
	}

	/**
	 * Returns the next token
	 * @return the next token
	 */
	public int nextToken() {
		return token;
	}

}
